<?php
class Person extends Zend_Db_Table_Abstract
{
    protected $_name = 'Person';
    
    // Check that the username is unique in the database
	function checkUnique($username)
    {
        $select = $this->_db->select()
			->from($this->_name,array('username'))
			->where('username=?',$username);
        $result = $this->getAdapter()->fetchOne($select);
        if($result){
            return true;
        }
        return false;
    }
	
	// Retrieve the username ID
	function getUserNameId($username)
    {
     	$select = $this->_db->select('idPerson')
     		->from($this->_name,array('idPerson')) 
     		->where('username=?',$username);
        $result = $this->getAdapter()->fetchOne($select);
        if($result){
            return $result;
        }
        return false;
    }
    
    // Retrieve the email from a username
    function getUserEmail($username) 
    {
    	$select = $this->_db->select('email')
    		->from($this->_name,array('email')) 
    		->where('username=?',$username);
        $result = $this->getAdapter()->fetchOne($select);
        if($result){
            return $result;
        }
        return false;
	}
    
	function setUserPassword($username, $password) 
	{
		// Prepare the data to be stored, ie hash the password
		$data = array(
			'password' => hash('sha256', $password)
		);
		
		// prepare the where statement
		$where = $this->_db->quoteInto("username = ?", $username);

		// Update the database
        $this->update($data, $where);
	}
	
	function getPersonSettings($idPerson)
	{
		$result = $this->fetchRow('idPerson = ' . $idPerson);
        if (!$result) {
			throw new Exception("Count not find row $idProject");
		}
		return $result->toArray();	
	}
	
	function setUserSettings($idPerson, $name, $email, $tel)
	{
		$data = array(
			'name' => $name,
			'email' => $email,
			'tel' => $tel,
		);
		$this->update($data,'idPerson='.(int)$idPerson);
	}
}

